import VueRouter from 'vue-router'
import Vue from 'vue'
import Login from '@/views/Login.vue'
import Home from '@/views/Home.vue'
import register from '@/components/register.vue'
import axios from 'axios'
import addGoods from '@/components/Goods/addGoods.vue'
import NavMain from '@/components/NavMain.vue'
import UPGoods from '@/components/Goods/UPGoods.vue'
import Personal_Central from '@/components/Personal_Central.vue'
import Goods from '@/components/Goods/Goods.vue'
import Good from '@/components/Goods/Good.vue'
Vue.use(VueRouter)
const routes = [
    {
        path: '/',
        component: Login,
        // redirect: Login
    },
    {
        path: '/home',
        component: Home,
        children: [
            {
                path: '/home/',
                component: NavMain
            }, {
                path: '/home/addGoods',
                component: addGoods
            }, {
                path: '/home/UPGoods',
                component: UPGoods
            }, {
                path: '/home/Personal_Central',
                component: Personal_Central
            }, {
                path: '/home/Goods',
                component: Goods
            }, {
                path: '/home/Good',
                component: Good
            }
        ]
    },
    {
        path: '/register',
        component: register,
    },
]

const router = new VueRouter({
    routes
})


router.beforeEach((to, from, next) => {
    // console.log('启动路由守卫' + JSON.parse(window.localStorage.getItem('access-admin')).token);
    // console.log('启动路由守卫' + JSON.parse(window.localStorage.getItem('access-admin')).data);
    if (to.path == '/') {
        next()
    } else {
        let admin = JSON.parse(window.localStorage.getItem('access-admin'))
        axios({
            url: 'http://localhost:8090/user/user/checkToken',
            methods: 'get',
            headers: {
                token: admin.token
            }
        }).then((response) => {
            console.log('token验证' + response.data);
            if (response.data) {
                console.log('校验成功');
                // next({ path: '/error' })
                next()
            } else {
                alert('token失效重新登陆')
                next({ path: '/Login' });
            }
        })
    }


    // if (to.path.startsWith('/')) {
    //     console.log('buzhou1');
    //     next()
    // } else {
    //     let admin = JSON.parse(window.localStorage.getItem('access-admin'))
    //     console.log('buzhou2');

    //     if (!admin) {
    //         next({ path: '/' })
    //     } else {
    //         axios({
    //             url: 'http://localhost:8090/user/user/checkToken',
    //             methods: 'get',
    //             headers: {
    //                 token: admin.token
    //             }
    //         }).then((response) => {
    //             console.log('token验证' + response.data);
    //             if (!response.data) {
    //                 console.log('校验失败');
    //                 // next({ path: '/error' })
    //             }
    //         })
    //         next()
    //     }
    // }
})

export default router